package com.cooperate.fly.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.nio.channels.FileChannel;

import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.WritableCellFormat;

/**
 * Created by baobao on 2016/6/30.
 */
public class FileUtil {

    public static void deleteAll(File f) {
        if(f==null || f.listFiles()==null) return;
        for (File fi : f.listFiles()) {
            if (fi.isDirectory()) {
                deleteAll(fi);
            } else {
                fi.delete();
            }
        }
        f.delete();
    }

    public static void writeToFile(String value, File f){
        int len = 0;
        char[] buffer = new char[1024];
        BufferedReader bufferedReader;
        BufferedWriter bufferedWriter = null;
        bufferedReader = new BufferedReader(new StringReader(value));
        try {

            bufferedWriter = new BufferedWriter(new FileWriter(f.getAbsolutePath()));
            while((len = bufferedReader.read(buffer))!=-1){
                bufferedWriter.write(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
                bufferedReader.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }

    }

    public static String readFileToString(File file){
        FileReader fr = null;
        BufferedReader bufr = null;
        String value = null;
        StringBuilder sb = new StringBuilder();
        try {
            fr = new FileReader(file);
            bufr = new BufferedReader(fr);
            while((value = bufr.readLine()) != null){
                sb.append(value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                fr.close();
                bufr.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public static void copyFile(File s, File t){

        FileInputStream fi = null;

        FileOutputStream fo = null;

        FileChannel in = null;

        FileChannel out = null;

        try {

            fi = new FileInputStream(s);

            fo = new FileOutputStream(t);

            in = fi.getChannel();//得到对应的文件通道

            out = fo.getChannel();//得到对应的文件通道

            in.transferTo(0, in.size(), out);//连接两个通道，并且从in通道读取，然后写入out通道

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                fi.close();

                in.close();

                fo.close();

                out.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    public static WritableCellFormat getHeader(){
        WritableCellFormat format = new WritableCellFormat();
        try{
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            format.setAlignment(Alignment.CENTRE);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            format.setBackground(Colour.BRIGHT_GREEN);
        }catch(Exception e){
            e.printStackTrace();
        }
        return format;
    }

    public static WritableCellFormat getHeader2(){
        WritableCellFormat format = new WritableCellFormat();
        try{
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            format.setAlignment(Alignment.CENTRE);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            format.setBackground(Colour.RED);
        }catch(Exception e){
            e.printStackTrace();
        }
        return format;
    }

    public static WritableCellFormat getCatalog(){
        WritableCellFormat format = new WritableCellFormat();
        try{
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            format.setAlignment(Alignment.CENTRE);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            format.setBackground(Colour.GREEN);
        }catch(Exception e){
            e.printStackTrace();
        }
        return format;
    }

    public static WritableCellFormat getData(){
        WritableCellFormat format = new WritableCellFormat();
        try{
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            format.setAlignment(Alignment.CENTRE);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            format.setBackground(Colour.GRAY_25);
        }catch(Exception e){
            e.printStackTrace();
        }
        return format;
    }
}
